package main

import (
	"fmt"
)

func main() {
	createSlices()
	copySlices()
	deleteElementFormSlices()
}

func printSlices(s []int) {
	fmt.Printf("%v, len = %d, cap = %d\n", s, len(s), cap(s))
}

// 创建slices
func createSlices() {
	fmt.Println("创建Slices")
	// 1.slices默认为空
	var s []int // Zero value for slices nil
	fmt.Println(s)
	for i := 0; i < 20; i++ {
		// 如果append不了，cap会扩大两倍
		printSlices(s)
		s = append(s, i*2+1)
	}
	fmt.Println(s)

	// 2.创建时添加value
	s1 := []int{2, 4, 6, 8}
	printSlices(s1)

	// 3.创建时指定len
	s2 := make([]int, 16)
	printSlices(s2)

	// 4.创建时指定len和cap
	s3 := make([]int, 16, 32)
	printSlices(s3)
}

// 拷贝Slices
func copySlices() {
	fmt.Println("拷贝Slices")

	// 2.创建时添加value
	s1 := []int{2, 4, 6, 8}
	printSlices(s1)

	// 3.创建时指定len
	s2 := make([]int, 16)
	printSlices(s2)

	copy(s2, s1)
	printSlices(s1)
	printSlices(s2)
}

// 删除元素
func deleteElementFormSlices() {
	fmt.Println("删除slice中特定的元素")
	s1 := []int{2, 4, 6, 8, 0, 0, 0}
	printSlices(s1)
	s1 = append(s1[:3], s1[4:]...)
	fmt.Println("删除中间的元素")
	printSlices(s1)

	fmt.Println("删除第一个")
	front := s1[0]
	s1 = s1[1:]
	fmt.Println(front)
	printSlices(s1)

	fmt.Println("删除最后一个")
	tail := s1[len(s1)-1]
	s1 = s1[:len(s1)-1]

	fmt.Println(tail)
	printSlices(s1)
}
